package cn.dankal.flyaudio.jpush;

import android.content.Intent;
import android.text.TextUtils;

import java.util.List;

import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.flyaudio.FlyApplication;
import cn.dankal.flyaudio.friend.ChatActivity;
import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.android.api.callback.GetUserInfoListCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;

public class JFriendManager {

    public static final void isMyFriend(String userName, GetMyFriendListener getMyFriendListener) {
        ContactManager.getFriendList(new GetUserInfoListCallback() {
            @Override
            public void gotResult(int i, String s, List<UserInfo> list) {
                for (UserInfo userInfo : list) {
                    if (TextUtils.equals(userName, userInfo.getUserName())) {
                        getMyFriendListener.isMyFriend(true);
                        return;
                    }
                }
                getMyFriendListener.isMyFriend(false);
            }
        });
    }

    public static void toChat(UserInfo userInfo) {
        Intent intent = new Intent();
        Conversation conv = Conversation.createSingleConversation(userInfo.getUserName(), userInfo.getAppKey());
        intent.putExtra(FlyApplication.CONV_TITLE, conv.getTitle());
        String targetId = ((UserInfo) conv.getTargetInfo()).getUserName();
        intent.putExtra(FlyApplication.TARGET_ID, targetId);
        intent.putExtra(FlyApplication.TARGET_APP_KEY, conv.getTargetAppKey());
        intent.setClass(ActivityUtils.getTopActivity(), ChatActivity.class);
        ActivityUtils.startActivity(intent);
    }

    public interface GetMyFriendListener {

        void isMyFriend(boolean isFriend);
    }
}
